package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Brand;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.BrandDao;
import com.weipai.springboot.module.mapper.BrandMapper;
import com.weipai.springboot.module.service.BrandService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:50
 */
@Service
public class BrandServiceImpl implements BrandService {

    private static final Logger log = LoggerFactory.getLogger(BrandServiceImpl.class);

    @Autowired
    private BrandDao brandDao;

    //保存
    @Override
    public Response save(Brand brand) {
        Response response = new Response();
        if(1==brandDao.insert(brand)){
            response.setCode(RetCode.SUCCESS);
        }else {
            log.debug("保存品牌失败:{}",brand.getBrandName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询品牌的详情
    @Override
    public Response getBrandById(String brandId) {
        Response response = new Response();
        Brand brand = brandDao.getBrandById(brandId);
        if(null!=brand){
            response.setCode(RetCode.SUCCESS);
            response.put("data", brand);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //获取数据（不包含逻辑删除的数据）
    @Override
    public List<Brand> selectAllByBrandDel(int brandDel, String search, String sort, String way) {
        return brandDao.selectAllByBrandDel(brandDel,search, sort, way);
    }

    //修改
    @Override
    public Response update(Brand brand) {
        Response response = new Response();
        brand.setUpdateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if(1==brandDao.updateByPrimaryKey(brand)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("修改品牌失败:{}",brand.getBrandName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //删除
    @Override
    public Response deleteById(String brandId) {
        Response response = new Response();
        Brand brand = brandDao.getBrandById(brandId);
        if(null!=brand){
            brand.setDelTime(DateUtils.timeFormat(System.currentTimeMillis()));
            brand.setBrandDel(-1);
            if(1==brandDao.updateByPrimaryKey(brand)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //更改品牌的状态
    @Override
    public Response changeStatus(String brandId, Integer status) {
        Response response = new Response();
        if(1==brandDao.updateStatus(brandId,status)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(brandDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //通过分类id查询此分类下的品牌
    @Override
    public Response getBrandByProCateId(String productCategoryId) {
        Response response = new Response();
        List<Brand> brands = brandDao.getBrandByProCateId(productCategoryId);
        if(brands.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.put("list",brands);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response getHotBrand() {
        Response response = new Response();
        List<Brand> brands =brandDao.getHotBrand();
        if(brands.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.put("list",brands);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }


}
